package com.starlight.flower_server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.flowerKnowledge.data.FlowerKnowledgeData;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.FlowerKnowledge;
import com.starlight.flower_server.domain.service.FlowerKnowledgeService;
import com.starlight.flower_server.domain.flowerKnowledge.cqrs.FlowerKnowledgePageQuery;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/5 15:02
 * 花百科
 */
@RestController
@RequestMapping("/flowerKnowledge")
public class FlowerKnowledgeController {
    @Resource
    private FlowerKnowledgeService flowerKnowledgeService;

    /**
     * 按id查询一项详细信息
     * @param id
     * @return
     */
    @GetMapping("/getFlowerKnowledgeDetailById")
    public FlowerServerResponse<FlowerKnowledge> getFlowerKnowledgeDetailById(Long id){
        return FlowerServerResponse.success(flowerKnowledgeService.queryFlowerKnowledgeDetailById(id));
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @PostMapping("/deleteFlowerKnowledgeById")
    @AuthenticationCheck(roleType = RoleType.ROOT)
    public FlowerServerResponse<Integer> deleteFlowerKnowledgeById(Long id){
        return FlowerServerResponse.success(flowerKnowledgeService.logicDeleteFlowerKnowledgeById(id));
    }

    /**
     * 添加记录
     * @param flowerKnowledge
     * @return
     */
    @PostMapping("/createFlowerKnowledge")
    public FlowerServerResponse<Integer> createFlowerKnowledge(@RequestBody FlowerKnowledge flowerKnowledge){
        return FlowerServerResponse.success(flowerKnowledgeService.insertFlowerKnowledge(flowerKnowledge));
    }

    /**
     * 更新记录
     * @param flowerKnowledge
     * @return
     */
    @PostMapping("/updateFlowerKnowledge")
//    @AuthenticationCheck(roleType = RoleType.ROLE_ROOT)
    public FlowerServerResponse<Integer> updateFlowerKnowledge(@RequestBody FlowerKnowledge flowerKnowledge){
        return FlowerServerResponse.success(flowerKnowledgeService.modifyFlowerKnowledge(flowerKnowledge));
    }

    /**
     * 分页查询
     * 包含
     * 查询分页
     * 模糊查询
     * 默认查询所有
     * 按照请求提供的参数进行条件查询
     * @param query
     * @return
     */
    @GetMapping("/getFlowerKnowledgePage")
    public FlowerServerResponse<IPage<FlowerKnowledgeData>> getFlowerKnowledgePage(FlowerKnowledgePageQuery query){
        return FlowerServerResponse.success(flowerKnowledgeService.queryFlowerKnowledgeList(query));
    }

    /**
     * 获取所有记录
     * @return
     */
    @GetMapping("/getAllFlowerKnowledgeData")
    public FlowerServerResponse<TotalData<FlowerKnowledgeData>> getAllFlowerKnowledgeData(){
        return FlowerServerResponse.success(flowerKnowledgeService.queryAllFlowerKnowledgeData());
    }

    /**
     * 获取记录总数
     * @return
     */
    @GetMapping("/getFlowerKnowledgeTotalCount")
    public FlowerServerResponse<TotalData<FlowerKnowledgeData>> getFlowerKnowledgeTotalCount(){
        return FlowerServerResponse.success(flowerKnowledgeService.queryFlowerKnowledgeTotalCount());
    }

    @GetMapping("/getRandomFlowerKnowledgeList")
    public FlowerServerResponse<List<FlowerKnowledgeData>> getRandomFlowerKnowledgeList(Integer count){
        if (count==null){
            throw new BadRequestException(Status.PARAMETER_ERROR.getMessage(), Status.PARAMETER_ERROR);
        }
        return FlowerServerResponse.success(flowerKnowledgeService.queryRandomFlowerKnowledgeList(count));
    }


}
